<?php


namespace app\console\controller;


use app\BaseController;

use app\console\service\supplier\SupplierQueryService;
use app\console\service\supplier\SupplierService;
use app\console\validate\SupplierValidate;
use app\lib\exception\ParamException;
use app\model\Supplier;
use app\Request;
use think\App;
use think\exception\ValidateException;

function supplier2dict($list){
    $dateTime = app('DateTime');

    return [
        'id' => $list['id'],
        'name' => $list['name'],
        'province' => $list['province'],
        'city' => $list['city'],
        'district' => $list['district'],
        'address' => $list['address'],
        'linkman' => $list['linkman'],
        'phone' => $list['phone'],
        'wechat' => $list['wechat'],
        'create_time' => $list['create_time'],
        'update_time' => $list['update_time'],
        'format_create_time' =>  $dateTime->timestamp2date($list['create_time']),
        'format_update_time' =>  $dateTime->timestamp2date($list['update_time']),
    ];
}
//供应商
class SupplierController extends BaseController
{

    public function create(Request $request, SupplierService $supplierService){
        $param = $request->param(['name', 'province', 'city', 'address', 'linkman', 'phone', 'wechat']);
        $district = $request->param('district', '');
        $param['district'] = $district;

        try
        {
            validate(SupplierValidate::class)
                ->scene('create')
                ->check($param);
        } catch (ValidateException $e) {
            throw new ParamException($e->getMessage());
        }

        $supplier = $supplierService->create($param);

        return $this->returnJson(supplier2dict($supplier));
    }

    public function get(Request $request, SupplierQueryService $supplierQueryService)
    {
        $all = $request->get('all', false);
        $page = $request->get('page', 1);
        $perPage = $request->get('perPage', config('app.allow_limit'));
        $keyword = $request->get('keyword', '');
        $province = $request->get('province', '');
        $city = $request->get('city', '');
        $district = $request->get('district', '');
        $search_start_time = $request->get('start_time', '');
        $search_end_time = $request->get('end_time', '');

        if (!$all)
        {
            try
            {
                validate(SupplierValidate::class)
                    ->scene('getPage')
                    ->check([
                        'page'         => $page ,
                        'perPage'      => $perPage ,
                        'search_start_time'   => $search_start_time ,
                        'search_end_time'     => $search_end_time ,
                    ]);

            } catch (ValidateException $e) {
                throw new ParamException($e->getMessage());
            }

            $supplier = $supplierQueryService->getPageList([
                'page'     =>  $page,
                'perPage'  =>  $perPage,
                'keyword'  =>  $keyword,
                'province' =>  $province,
                'city'     =>  $city,
                'district'     =>  $district,
                'search_start_time'  =>  $search_start_time,
                'search_end_time'    =>  $search_end_time
            ]);
        }else{
            try
            {
                validate(SupplierValidate::class)
                    ->scene('getAll')
                    ->check([
                        'search_start_time'   => $search_start_time ,
                        'search_end_time'     => $search_end_time ,
                    ]);
            } catch (ValidateException $e) {
                throw new ParamException($e->getMessage());
            }

            $supplier = $supplierQueryService->getList([
                'keyword'  =>  $keyword,
                'province' =>  $province,
                'city'     =>  $city,
                'district'     =>  $district,
                'search_start_time'  =>  $search_start_time,
                'search_end_time'    =>  $search_end_time
            ]);
        }

        return $this->returnList(
            array_map(fn($list) => supplier2dict($list), $supplier['list']->toArray()),
            $supplier['pagination']['page'], $supplier['pagination']['perPage'], $supplier['pagination']['total']
        );
    }

    public function update(Request $request, SupplierService $supplierService)
    {
        $param = $request->param(['name', 'province', 'city', 'province',
            'district', 'address', 'linkman', 'phone', 'wechat'], '');

        if (empty($param)) {
            throw new ParamException('未修改任何数据');
        }

        $supplier = $supplierService->update($param);

        return $this->returnJson(supplier2dict($supplier));
    }

    public function delete(SupplierService $supplierService)
    {
        $supplierService->delete();
        return $this->returnJson();
    }

    public function getById(Supplier $supplier)
    {
        return $this->returnJson(supplier2dict($supplier));
    }

}